<?php

namespace Home\Controller;

class IndexController extends BaseController {

    //商品列表
    public function personsList() {
//        $this->error('数据维护中...');
        
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        isset($postData['key']) && $where['nickname'] = array('like', "%{$postData['key']}%");
        if (!empty($postData['age'])) {
            $ageArr = explode('-', $postData['age']);
            $where['age'][] = array('EGT', $ageArr[0]);
            $where['age'][] = array('LT', $ageArr[1]);
        }
        if (!empty($postData['height'])) {
            $heightArr = explode('-', $postData['height']);
            $where['height'][] = array('EGT', $heightArr[0]);
            $where['height'][] = array('LT', $heightArr[1]);
        }
        if (!empty($postData['weight'])) {
            $weightArr = explode('-', $postData['weight']);
            $where['weight'][] = array('EGT', $weightArr[0]);
            $where['weight'][] = array('LT', $weightArr[1]);
        }
        if (!empty($postData['price'])) {
            $priceArr = explode('-', $postData['price']);
            $where['price'][] = array('EGT', $priceArr[0]);
            $where['price'][] = array('ELT', $priceArr[1]);
        }
        
        isset($postData['gender']) && $where['gender'] = $postData['gender'];
        
        isset($postData['bust']) && $where['bust'] = $postData['bust'];
        !empty($postData['city']) && $where['city'] = array("IN",[$postData['city'], '']);
        !empty($postData['ishome']) && $where['ishome'] = $postData['ishome'];

        $search_type = intval($postData['search_type']) ? intval($postData['search_type']) : 0;
        $sort = intval($postData['sort']) ? intval($postData['sort']) : '';

        $where['state'] = 1;
        $field = "s.wid,s.nickname,s.headpic,s.age,s.height,s.weight,s.bust,s.price,s.xingzuo,s.old_address,s.address,s.detail_address,s.gender,"
                . "s.ishome,s.update_time,s.create_time,s.isonwork,s.state,s.intro,s.lon,s.lat,s.video_img,s.arr_img,s.video_url,s.city,"
                . "top,auth_state,s.tags,s.appearance,s.zodiac,s.idol,s.hobby,s.job,s.income,s.house,s.blood";

        $lat = I('lat');
        $lon = I('lon');
        //1综合 2距离 3价格 4好评率
        //sort:1 高-低 2 低-高
        switch ($search_type) {
            case 1:
                empty($lat) && $this->error('请传定位经纬度');
                empty($lon) && $this->error('请传定位经纬度');
                $field .= ",(round(6367000 * 2 * asin(sqrt(pow(sin(((lat * pi()) / 180 - ({$lat} * pi()) / 180) / 2), 2) + cos(({$lat} * pi()) / 180) * cos((lat * pi()) / 180) * pow(sin(((lon * pi()) / 180 - ({$lon} * pi()) / 180) / 2), 2))))) AS distance";
                $sort = "top DESC, distance ASC";
                break;
            case 2:
                empty($lat) && $this->error('请传定位经纬度');
                empty($lon) && $this->error('请传定位经纬度');
                $field .= ",(round(6367000 * 2 * asin(sqrt(pow(sin(((lat * pi()) / 180 - ({$lat} * pi()) / 180) / 2), 2) + cos(({$lat} * pi()) / 180) * cos((lat * pi()) / 180) * pow(sin(((lon * pi()) / 180 - ({$lon} * pi()) / 180) / 2), 2))))) AS distance";
                $sort = "distance ASC";
                break;
            case 3:
                if ($sort == 1) {
                    $sort = "price DESC";
                } else {
                    $sort = "price ASC";
                }
                break;
            case 4:
                $field .= ",avg(attitude_star+face_star) as avg_scroe";
                $sort = "avg_scroe DESC";
                $group = "wid";
                $join = "left join ot_order_eval as oe ON oe.wid = s.wid";
                break;
            case 5:
                $sort = "wid DESC";
                break;
            default:
                empty($lat) && $this->error('请传定位经纬度');
                empty($lon) && $this->error('请传定位经纬度');
                $field .= ",(round(6367000 * 2 * asin(sqrt(pow(sin(((lat * pi()) / 180 - ({$lat} * pi()) / 180) / 2), 2) + cos(({$lat} * pi()) / 180) * cos((lat * pi()) / 180) * pow(sin(((lon * pi()) / 180 - ({$lon} * pi()) / 180) / 2), 2))))) AS distance";
                $sort = "top DESC, distance ASC";
                break;
        }

        $list = M('seller')->alias("s")
                ->field($field)
                ->join($join)
                ->where($where)
                ->order($sort)
                ->group($group)
                ->limit($size)
                ->page($page)
                ->select();
        if (!empty($list)) {
            foreach ($list as &$val) {
                if (!empty($val['arr_img'])) {
                    $val['arr_img'] = explode(',', subOssImgUrl($val['arr_img']));
                }
                $val['headpic'] = subOssImgUrl($val['headpic']);
                $val['video_img'] = subOssImgUrl($val['video_img']);
                $val['video_url'] = subOssImgUrl($val['video_url']);
                $val['scroe'] = getAvgScore($val['wid']);
            }
        }
        $this->returnList($list);
    }

    //商品详情
    public function personDetail() {
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '选择商品ID');
        $field = "wid,business_phone,nickname,headpic,age,height,weight,bust,price,xingzuo,old_address,address,detail_address,ishome,arr_img,update_time,create_time,isonwork,state,video_url,video_img,intro,lon,lat,top,auth_state,remark,qq,qqnum,receivenum,wx,activityid,gender,recommend_uid";
        $field .= ",movie,music,book,motion,game,place,tags,appearance,zodiac,idol,hobby,job,income,house,blood";
        $lat = I('lat');
        $lon = I('lon');
        if ($lat && $lon) {
            $field .= ",(round(6367000 * 2 * asin(sqrt(pow(sin(((lat * pi()) / 180 - ({$lat} * pi()) / 180) / 2), 2) + cos(({$lat} * pi()) / 180) * cos((lat * pi()) / 180) * pow(sin(((lon * pi()) / 180 - ({$lon} * pi()) / 180) / 2), 2))))) AS distance";
        }
        $sellerInfo = M('seller')->field($field)->where(['wid' => $wid])->find();
        if ($sellerInfo) {
            if (!empty($sellerInfo['arr_img'])) {
                $sellerInfo['arr_img'] = explode(',', subOssImgUrl($sellerInfo['arr_img']));
            }
            $sellerInfo['score'] = getAvgScore($wid);
            $collectionInfo = M("user_collection")->where(['user_id' => $this->user_id, 'wid' => $wid])->find();
            if (!empty($collectionInfo)) {
                $sellerInfo['iscollect'] = 1;
            } else {
                $sellerInfo['iscollect'] = 0;
            }
            $sellerInfo['headpic'] = subOssImgUrl($sellerInfo['headpic']);
            $sellerInfo['video_img'] = subOssImgUrl($sellerInfo['video_img']);
            $sellerInfo['video_url'] = subOssImgUrl($sellerInfo['video_url']);
        }
        $this->returnList($sellerInfo);
    }

    //商品评价列表
    public function evalutionList() {
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '选择商品ID');
        $list = M('order_eval')->where(['wid' => $wid])->order('eval_id DESC')->limit($size)->page($page)->select();
        $this->returnList($list);
    }

    //动态列表
    public function dynamicList() {
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $wid = $this->getParam('wid', 0);
        $where = array();
        if (!empty($wid)) {
            $where["sc.wid"] = $wid;
        }
        !empty($postData['city']) && $where['sc.city'] = array("IN",[$postData['city'], '']);
        !empty($postData['type']) && $where['sc.type'] = $postData['type'];
        
        $list = M('seller_content')
                ->alias('sc')
                ->field('sc.*,s.nickname,s.headpic,s.lon,s.lat')
                ->join('ot_seller AS s ON s.wid = sc.wid')
                ->where($where)
                ->order('sc.top DESC, sc.id DESC')
                ->limit($size)
                ->page($page)
                ->select();
        if ($list) {
            foreach ($list as &$val) {
                if (!empty($val['img_path'])) {
                    $val['img_path'] = explode(',', subOssImgUrl($val['img_path']));
                }
                $val['headpic'] = subOssImgUrl($val['headpic']);
                $val['video_img'] = subOssImgUrl($val['video_img']);
                $val['video_url'] = subOssImgUrl($val['video_url']);
            }
            unset($val);
        }
        $this->returnList($list);
    }

}
